#!/usr/bin/python -S
"""
break.py

For testing breaking of the input stream, i.e. "partial reads".
"""

__author__ = 'Andy Chu'


import sys
import time


class Error(Exception):
  pass


def main(argv):
  """Returns an exit code."""
  try:
    chunk_size = int(argv[1])
  except IndexError:
    chunk_size = 3
  try:
    seconds = float(argv[2])
  except IndexError:
    seconds = 0.5

  while True:
    bytes = sys.stdin.read(chunk_size)
    if not bytes:
      break
    time.sleep(seconds)
    sys.stdout.write(bytes)
    sys.stdout.flush()
  return 0


if __name__ == '__main__':
  try:
    sys.exit(main(sys.argv))
  except Error, e:
    print >> sys.stderr, e.args[0]
    sys.exit(1)
